using System;
using System.Xml;
using System.Data;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace Weathertastic
{
    class Settings
    {
        private string _stationCode = null;
        private string _locationName = null;
        private bool _useImperial;
        private bool _showBalloons;
        private bool _showForecasts;
        private bool _checkForUpdates;
        private bool _useProxy;
        private string _source = null;
        private string _theme = null;

        private Engine _sender = null;


        private XmlDocument xFile = new XmlDocument();

        public event EventHandler SettingsUpdated;

        public Settings() { LoadSettings();  }

        public Settings(Engine sender)
        {
            LoadSettings();
            _sender = (Engine)sender;

        }


        #region variable
        [Category("Weather"), Description("Your weather.com station code")]
        public string StationCode
        {
            get { return _stationCode; }
            set { _stationCode = value; }
        }

        [Category("Weather"), Description("Icon theme")]
        public string Theme
        {
            get { return _theme; }
            set { _theme = value; }
        }

        [Category("Weather"), Description("Location name")]
        public string LocationName
        {
            get { return _locationName; }
            set { _locationName = value; }
        }

        [Category("Weather"), Description("Weather source")]
        public string Source
        {
            get { return _source; }
            set { _source = value; }
        }

        [Category("Weather"), Description("Set to true to use imperial units, or false to use metric units")]
        public bool UseImperial
        {
            get { return _useImperial; }
            set { _useImperial = value; }
        }

        [Category("Weather"), Description("Set to true to show a 7 day forecast, set to false to just show current weather.")]
        public bool ShowForecasts
        {
            get { return _showForecasts; }
            set { _showForecasts = value; }
        }

        [Category("Misc"), Description("Do you want to show balloons with the new weather information?")]
        public bool ShowBalloonTips
        {
            get { return _showBalloons; }
            set { _showBalloons = value; }
        }

        [Category("Misc"), Description("Allow Weathertastic to automatically check for updates?")]
        public bool CheckForUpdates
        {
            get { return _checkForUpdates; }
            set { _checkForUpdates = value; }
        }

        [Category("Misc"), Description("Should Weathertastic use your default proxy settings, as specified by IE?")]
        public bool UseProxy
        {
            get { return _useProxy; }
            set { _useProxy = value; }
        }
        #endregion

        /* Writes out settings to xml file */
        public void SaveSettings()
        {
            try
            {

                xFile = new XmlDocument();
                xFile.Load("settings.xml");

                xFile.SelectSingleNode("//station").InnerText = _stationCode;
                xFile.SelectSingleNode("//location_name").InnerText = _locationName;
                xFile.SelectSingleNode("//use_imperial").InnerText = _useImperial.ToString();
                //xFile.SelectSingleNode("//show_balloons").InnerText = _showBalloons.ToString();
                //xFile.SelectSingleNode("//show_forecasts").InnerText = _showForecasts.ToString();
                //xFile.SelectSingleNode("//check_for_updates").InnerText = _checkForUpdates.ToString();

                xFile.SelectSingleNode("//use_proxy").InnerText = _useProxy.ToString();
                xFile.SelectSingleNode("//theme").InnerText = _theme;

                xFile.Save("settings.xml");

                xFile = null;

                if (_sender != null)
                {
                    _sender.UpdateWeather(null, null);
                }


            }
            catch (Exception exc)
            {
                throw;
            }
        }

        public void LoadSettings()
        {
            try
            {
                xFile.Load(Path.Combine(Application.StartupPath, "settings.xml"));
                XmlNode temp = xFile.SelectSingleNode("//root");

                // Check to see if new values are there
                if (temp.SelectSingleNode("//use_proxy") == null)
                {
                    XmlNode _newNode = xFile.DocumentElement;
                    XmlElement _newElement = xFile.CreateElement("use_proxy");
                    _newElement.InnerText = false.ToString();
                    temp.AppendChild(_newElement);
                    xFile.Save("settings.xml");
                }

                if (temp.SelectSingleNode("//use_imperial") == null)
                {
                    XmlNode _newNode = xFile.DocumentElement;
                    XmlElement _newElement = xFile.CreateElement("use_imperial");
                    _newElement.InnerText = false.ToString();
                    temp.AppendChild(_newElement);
                    xFile.Save("settings.xml");
                }

                if (temp.SelectSingleNode("//location_name") == null)
                {
                    XmlNode _newNode = xFile.DocumentElement;
                    XmlElement _newElement = xFile.CreateElement("location_name");
                    _newElement.InnerText = string.Empty;
                    temp.AppendChild(_newElement);
                    xFile.Save("settings.xml");
                }

                if (temp.SelectSingleNode("//station") == null)
                {
                    XmlNode _newNode = xFile.DocumentElement;
                    XmlElement _newElement = xFile.CreateElement("station");
                    _newElement.InnerText = string.Empty;
                    temp.AppendChild(_newElement);
                    xFile.Save("settings.xml");
                }

                if (temp.SelectSingleNode("//theme") == null)
                {
                    XmlNode _newNode = xFile.DocumentElement;
                    XmlElement _newElement = xFile.CreateElement("theme");
                    _newElement.InnerText = string.Empty;
                    temp.AppendChild(_newElement);
                    xFile.Save("settings.xml");
                }

                _stationCode = temp.SelectSingleNode("//station").InnerText;
                _useImperial = Convert.ToBoolean(temp.SelectSingleNode("//use_imperial").InnerText);
               // _checkForUpdates = Convert.ToBoolean(temp.SelectSingleNode("//check_for_updates").InnerText);
                _useProxy = Convert.ToBoolean(temp.SelectSingleNode("//use_proxy").InnerText);
                _locationName = temp.SelectSingleNode("//location_name").InnerText;
                _theme = temp.SelectSingleNode("//theme").InnerText;

                temp = null;
                xFile = null;
            }

            catch (Exception exc)
            {
                throw;
            }
        }

        // FIXME
        protected virtual void OnSettingsUpdated()
        {
            try
            {
                this.SettingsUpdated(this, null);

            }

            catch (Exception exc)
            {
                throw;
            }
        }





    }
}
